/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.marcelo.comun.database;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author marcelo
 */
public class CSVWriter {
    FileWriter fileWriter;
    String caracterSeparacion;
    List<String> datos;

    private CSVWriter () {
        datos = new ArrayList<String>();
    }
    public CSVWriter(FileWriter fileWriter) {
        this();
        this.fileWriter = fileWriter;
    }

    public CSVWriter(File file, boolean append, String caracterSeparacion) throws IOException{
        this();
        fileWriter = new FileWriter(file,append);
        this.caracterSeparacion = caracterSeparacion;
    }
    
    public void write(String data) {
        datos.add(data);
    }
    public void endRecord() throws IOException{
        // Esta mal esta implementacion
        String linea ="";
        for (String s : datos) {
            if (!linea.equals(""))
                linea += caracterSeparacion;
            linea += s;
        }
        fileWriter.write(linea);
        datos.clear();
    }
    
    public void close() throws IOException{
        fileWriter.flush();
        fileWriter.close();
    }
    
    
}
